<?php
	
	require_once 'Validate.php';
	
	class ForumHandler {
		
		private $db;
		
		public function __construct(Database $db) {
			$this->db = $db;
		}
		
		//Funktioner för att spara ämnen och svar.
		public function SaveNewTopic($topic, $detail, $user) {
			
			$val = new Validate();
			$date = $this->GetTime();

			$query = "SELECT id FROM user WHERE Username = ?";
			$stmt = $this->db->Prepare($query);	
			$stmt->bind_param('s', $user);	
			$stmt = $this->db->ExecuteIt($stmt);			
			$stmt->bind_result($userId);		
			$stmt->fetch();		
			$stmt->close();
			
			if ($topic == null || $detail == null) {
				return false;
			}
			else if ($val->NoCode($topic) == false || $val->NoCode($detail) == false) {
				return false;
			}
			else {
				$sql = "INSERT INTO question (UserId, Topic, Detail, Date) VALUES (?, ?, ?, ?)";
				$stmt = $this->db->Prepare($sql);
				$stmt->bind_param('isss', $userId, $topic, $detail, $date);
				$query = $this->db->Insert($stmt);
				return true;
			}
		}
		
		public function SaveAnswer($qId, $text, $user) {
			
			$val = new Validate();
			$date = $this->GetTime();
			
			$query = "SELECT id FROM user WHERE Username = ?";
			$stmt = $this->db->Prepare($query);	
			$stmt->bind_param('s', $user);	
			$stmt = $this->db->ExecuteIt($stmt);
			$stmt->bind_result($userId);		
			$stmt->fetch();		
			$stmt->close();
			
			if ($text == null) {
				return false;
			}
			else if ($val->NoCode($text) == false) {
				return false;
			}
			else {
				$sql = "INSERT INTO answer (qId, Text, uId, Date) VALUES (?, ?, ?, ?)";
				$stmt = $this->db->Prepare($sql);
				$stmt->bind_param('isis', $qId, $text, $userId, $date);
				$query = $this->db->Insert($stmt);				
				return true;
			}
		}
		
		//Redigera topic.
		public function EditTopic($id, $text) {
				
			$val = new Validate();
					
			if ($text == null) {
				return false;
			}
			else if ($val->NoCode($text) == false) {
				return false;
			}
			else {
			
				$query = "UPDATE question SET Detail = '$text' WHERE id = ?";
				$stmt = $this->db->Prepare($query);
				$stmt->bind_param('i', $id);
				$query = $this->db->ExecuteIt($stmt);
				return true;
			}
		}
		
		//Hämtar alla ämnen.
		public function GetAllTopics() {

			$result = "";
			$query = "SELECT q.id, u.Username, q.Topic, q.Detail, q.Date
				FROM question AS q INNER JOIN user AS u
				ON q.userId = u.id";
			$stmt = $this->db->Prepare($query);
			$stmt = $this->db->ExecuteIt($stmt);			
			$stmt->bind_result($id, $uName, $topic, $detail, $date);
			
			while ($stmt->fetch()) {
				$result[$id] = array('username' => $uName, 'topic' => $topic, 'detail' => $detail, 'date' => $date);
			}
			
			$stmt->close();
			
			return $result;
		}
		
		//Hämtar tiden.
		public function GetTime() {
			
			$timezone = date_default_timezone_get();		
			date_default_timezone_set($timezone);
			$date = date('Y-m-d h:i:s', time());
			
			return $date;
		}
		
		//Skriver ut detaljer för det ämnet man väljer.
		public function SelectedTopicDetail($id) {
			
			$query = "SELECT q.Topic, q.Detail, q.Date, u.Username
				FROM question AS q INNER JOIN user AS u
				ON q.UserId = u.id WHERE q.id = ?";
			$stmt = $this->db->Prepare($query);
			$stmt->bind_param('i', $id);
			$stmt = $this->db->ExecuteIt($stmt);
			$stmt->bind_result($topic, $text, $date, $username);
			
			$result = ""; 
			
			while ($stmt->fetch()) {
				$result[$id] = array('topic' => $topic, 'text' => $text, 'date' => $date, 'username' => $username);
			}
			$stmt->close();
			
			return $result;
		}
		
		//Skriver ut alla svar på ett visst ämne.
		public function GetAllAnswers($id) {
			
			$query = "SELECT a.id, a.Text, u.Username, a.Date 
				FROM answer AS a INNER JOIN user AS u
				ON a.uId = u.Id WHERE a.qId = ?";
			$stmt = $this->db->Prepare($query);
			$stmt->bind_param('i', $id);
			$stmt = $this->db->ExecuteIt($stmt);		
			$stmt->bind_result($id, $text, $user, $date);
			
			$result = ""; 
			
			while ($stmt->fetch()) {
				$result[$id] = array('id' => $id, 'text' => $text, 'username' => $user, 'date' => $date);
			}
			$stmt->close();
			
			return $result;
		}
		
		//Funktioner för delete.
		public function DeleteAnswer($id) {
			
			$query = "DELETE FROM answer WHERE id = ?";
			$stmt = $this->db->Prepare($query);
			$stmt->bind_param('i', $id);
			$stmt = $this->db->Delete($stmt);	
		}
		
		public function DeleteTopic($id) {
			
			$query = "DELETE FROM question WHERE id = ?";
			$stmt = $this->db->Prepare($query);
			$stmt->bind_param('i', $id);
			$stmt = $this->db->Delete($stmt);	
		}
		
		//Kollar om rätt användare försöker radera.
		public function CheckUser($questionId) {
			
			$query = "SELECT u.Username
				FROM user AS u INNER JOIN question AS q
				ON q.UserId = u.Id WHERE q.Id = ?";
			$stmt = $this->db->Prepare($query);
			$stmt->bind_param('i', $questionId);
			$stmt = $this->db->ExecuteIt($stmt);
			
			$stmt->bind_result($result);		
			$stmt->fetch();		
			$stmt->close();	
			
			return $result;
		}
		
		public function CheckUserForAnswer($answerId) {
			
			$query = "SELECT u.Username
				FROM user AS u INNER JOIN answer AS a
				ON a.UId = u.id WHERE a.Id = ?";
			$stmt = $this->db->Prepare($query);
			$stmt->bind_param('i', $answerId);
			$stmt = $this->db->ExecuteIt($stmt);
			
			$stmt->bind_result($result);		
			$stmt->fetch();		
			$stmt->close();	
			
			return $result;
		}
		
		public function Test() {
			
			//Tester för att spara.
			if ($this->SaveNewTopic(null, null, "Britt-Marie") == true) {
				echo "SaveNewTopic-test failed.";
				return false;
			}			
			if ($this->SaveAnswer(1, null, "Britt-Marie")) {
				echo "SaveAnswer-test failed.";
				return false;
			}
			
			//Select-tester.
			$row = $this->SelectedTopicDetail(21);
			if ($row == null) {
				echo "SelectedTopicDetail-test failed.";
				return false;
			}
			$row = $this->GetAllTopics();
			if ($row < 0) {
				echo "GetAllTopics-test failed.";
				return false;
			}
			$row = $this->GetAllAnswers(21);
			if ($row < 0) {
				echo "GetAllAnswers-test failed.";
				return false;
			}
			
			//Tester för delete.
			$row = $this->DeleteTopic(1);
			if ($row == 1) {
				echo "DeleteTopic-test failed.";
				return false;
			}
			$row = $this->DeleteAnswer(1);
			if ($row == 1) {
				echo "DeleteTopic-test failed.";
				return false;
			}
			
			return true;	
		}
	}
